﻿namespace TestTool.GUI.Controls
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    using TestTool.GUI.Controllers;
    using TestTool.GUI.Controls.Device;
    using TestTool.GUI.Data;
    using TestTool.GUI.Enums;
    using TestTool.GUI.Views;
    using TestTool.HttpTransport.Interfaces;

    internal class DevicePage : Page, IDeviceView, IView
    {
        private DeviceController _controller;
        private bool _notUserAction;
        private IContainer components;
        private DeviceManagementPage deviceManagementPage;
        private GroupBox gbAuthentication;
        private GroupBox gbCapabilitiesExchange;
        private MediaPage mediaPage;
        private Panel panelSpace;
        private Panel panelTop;
        private PtzPage ptzPage;
        private RadioButton rbDigest;
        private RadioButton rbGetCapabilities;
        private RadioButton rbGetServices;
        private RadioButton rbNone;
        private RadioButton rbWsUsername;
        private RequestsPage requestsPage;
        private TabControl tcDevice;
        private TabPage tpDeviceManagement;
        private TabPage tpMedia;
        private TabPage tpPtz;
        private TabPage tpRequests;

        public DevicePage()
        {
            this.InitializeComponent();
            this._controller = new DeviceController(this);
            this._controller.SetChildControllers(this.deviceManagementPage.Controller, this.mediaPage.Controller, this.ptzPage.Controller, this.requestsPage.Controller);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        public override IController GetController()
        {
            return this._controller;
        }

        private void InitializeComponent()
        {
            this.tcDevice = new TabControl();
            this.tpDeviceManagement = new TabPage();
            this.deviceManagementPage = new DeviceManagementPage();
            this.tpMedia = new TabPage();
            this.mediaPage = new MediaPage();
            this.tpPtz = new TabPage();
            this.ptzPage = new PtzPage();
            this.tpRequests = new TabPage();
            this.requestsPage = new RequestsPage();
            this.gbCapabilitiesExchange = new GroupBox();
            this.rbGetServices = new RadioButton();
            this.rbGetCapabilities = new RadioButton();
            this.gbAuthentication = new GroupBox();
            this.rbDigest = new RadioButton();
            this.rbWsUsername = new RadioButton();
            this.rbNone = new RadioButton();
            this.panelTop = new Panel();
            this.panelSpace = new Panel();
            this.tcDevice.SuspendLayout();
            this.tpDeviceManagement.SuspendLayout();
            this.tpMedia.SuspendLayout();
            this.tpPtz.SuspendLayout();
            this.tpRequests.SuspendLayout();
            this.gbCapabilitiesExchange.SuspendLayout();
            this.gbAuthentication.SuspendLayout();
            this.panelTop.SuspendLayout();
            base.SuspendLayout();
            this.tcDevice.Controls.Add(this.tpDeviceManagement);
            this.tcDevice.Controls.Add(this.tpMedia);
            this.tcDevice.Controls.Add(this.tpPtz);
            this.tcDevice.Controls.Add(this.tpRequests);
            this.tcDevice.Dock = DockStyle.Fill;
            this.tcDevice.Location = new Point(0, 0x33);
            this.tcDevice.Name = "tcDevice";
            this.tcDevice.SelectedIndex = 0;
            this.tcDevice.Size = new Size(0x2e2, 0x1c0);
            this.tcDevice.TabIndex = 0;
            this.tpDeviceManagement.Controls.Add(this.deviceManagementPage);
            this.tpDeviceManagement.Location = new Point(4, 0x16);
            this.tpDeviceManagement.Name = "tpDeviceManagement";
            this.tpDeviceManagement.Padding = new Padding(3);
            this.tpDeviceManagement.Size = new Size(730, 0x1a6);
            this.tpDeviceManagement.TabIndex = 0;
            this.tpDeviceManagement.Text = "Device Management";
            this.tpDeviceManagement.UseVisualStyleBackColor = true;
            this.deviceManagementPage.Dock = DockStyle.Fill;
            this.deviceManagementPage.Location = new Point(3, 3);
            this.deviceManagementPage.Name = "deviceManagementPage";
            this.deviceManagementPage.Size = new Size(0x2d4, 0x1a0);
            this.deviceManagementPage.TabIndex = 0;
            this.tpMedia.Controls.Add(this.mediaPage);
            this.tpMedia.Location = new Point(4, 0x16);
            this.tpMedia.Name = "tpMedia";
            this.tpMedia.Padding = new Padding(3);
            this.tpMedia.Size = new Size(730, 0x1a6);
            this.tpMedia.TabIndex = 1;
            this.tpMedia.Text = "Media";
            this.tpMedia.UseVisualStyleBackColor = true;
            this.mediaPage.Dock = DockStyle.Fill;
            this.mediaPage.Location = new Point(3, 3);
            this.mediaPage.Name = "mediaPage";
            this.mediaPage.Size = new Size(0x2d4, 0x1a0);
            this.mediaPage.TabIndex = 0;
            this.tpPtz.Controls.Add(this.ptzPage);
            this.tpPtz.Location = new Point(4, 0x16);
            this.tpPtz.Name = "tpPtz";
            this.tpPtz.Padding = new Padding(3);
            this.tpPtz.Size = new Size(730, 0x1a6);
            this.tpPtz.TabIndex = 2;
            this.tpPtz.Text = "PTZ";
            this.tpPtz.UseVisualStyleBackColor = true;
            this.ptzPage.Dock = DockStyle.Fill;
            this.ptzPage.Location = new Point(3, 3);
            this.ptzPage.Name = "ptzPage";
            this.ptzPage.Size = new Size(0x2d4, 0x1a0);
            this.ptzPage.TabIndex = 0;
            this.tpRequests.Controls.Add(this.requestsPage);
            this.tpRequests.Location = new Point(4, 0x16);
            this.tpRequests.Name = "tpRequests";
            this.tpRequests.Size = new Size(730, 0x1a6);
            this.tpRequests.TabIndex = 3;
            this.tpRequests.Text = "Requests";
            this.tpRequests.UseVisualStyleBackColor = true;
            this.requestsPage.Dock = DockStyle.Fill;
            this.requestsPage.Location = new Point(0, 0);
            this.requestsPage.Name = "requestsPage";
            this.requestsPage.Service = DutService.DeviceManagement;
            this.requestsPage.Size = new Size(730, 0x1a6);
            this.requestsPage.TabIndex = 0;
            this.gbCapabilitiesExchange.Controls.Add(this.rbGetServices);
            this.gbCapabilitiesExchange.Controls.Add(this.rbGetCapabilities);
            this.gbCapabilitiesExchange.Dock = DockStyle.Left;
            this.gbCapabilitiesExchange.Location = new Point(0, 0);
            this.gbCapabilitiesExchange.Name = "gbCapabilitiesExchange";
            this.gbCapabilitiesExchange.Size = new Size(0x14c, 0x2e);
            this.gbCapabilitiesExchange.TabIndex = 1;
            this.gbCapabilitiesExchange.TabStop = false;
            this.gbCapabilitiesExchange.Text = "Capabilities Exchange";
            this.rbGetServices.AutoSize = true;
            this.rbGetServices.Checked = true;
            this.rbGetServices.Location = new Point(0xb0, 0x13);
            this.rbGetServices.Name = "rbGetServices";
            this.rbGetServices.Size = new Size(0x83, 0x11);
            this.rbGetServices.TabIndex = 1;
            this.rbGetServices.TabStop = true;
            this.rbGetServices.Text = "GetServices";
            this.rbGetServices.UseVisualStyleBackColor = true;
            this.rbGetServices.CheckedChanged += new EventHandler(this.rb_CheckedChanged);
            this.rbGetCapabilities.AutoSize = true;
            this.rbGetCapabilities.Location = new Point(7, 0x13);
            this.rbGetCapabilities.Name = "rbGetCapabilities";
            this.rbGetCapabilities.Size = new Size(0x95, 0x11);
            this.rbGetCapabilities.TabIndex = 0;
            this.rbGetCapabilities.Text = "GetCapabilities";
            this.rbGetCapabilities.UseVisualStyleBackColor = true;
            this.rbGetCapabilities.CheckedChanged += new EventHandler(this.rb_CheckedChanged);
            this.gbAuthentication.Controls.Add(this.rbDigest);
            this.gbAuthentication.Controls.Add(this.rbWsUsername);
            this.gbAuthentication.Controls.Add(this.rbNone);
            this.gbAuthentication.Dock = DockStyle.Fill;
            this.gbAuthentication.Location = new Point(0x14c, 0);
            this.gbAuthentication.Name = "gbAuthentication";
            this.gbAuthentication.Size = new Size(0x196, 0x2e);
            this.gbAuthentication.TabIndex = 2;
            this.gbAuthentication.TabStop = false;
            this.gbAuthentication.Text = "Authentication";
            this.rbDigest.AutoSize = true;
            this.rbDigest.Checked = true;
            this.rbDigest.Location = new Point(0xe9, 0x13);
            this.rbDigest.Name = "rbDigest";
            this.rbDigest.Size = new Size(0x37, 0x11);
            this.rbDigest.TabIndex = 2;
            this.rbDigest.TabStop = true;
            this.rbDigest.Text = "Digest";
            this.rbDigest.UseVisualStyleBackColor = true;
            this.rbDigest.CheckedChanged += new EventHandler(this.rb_CheckedChanged);
            this.rbWsUsername.AutoSize = true;
            this.rbWsUsername.Location = new Point(0x54, 0x13);
            this.rbWsUsername.Name = "rbWsUsername";
            this.rbWsUsername.Size = new Size(0x7c, 0x11);
            this.rbWsUsername.TabIndex = 1;
            this.rbWsUsername.Text = "WS-Username token";
            this.rbWsUsername.UseVisualStyleBackColor = true;
            this.rbWsUsername.CheckedChanged += new EventHandler(this.rb_CheckedChanged);
            this.rbNone.AutoSize = true;
            this.rbNone.Location = new Point(6, 0x13);
            this.rbNone.Name = "rbNone";
            this.rbNone.Size = new Size(0x33, 0x11);
            this.rbNone.TabIndex = 0;
            this.rbNone.Text = "None";
            this.rbNone.UseVisualStyleBackColor = true;
            this.rbNone.CheckedChanged += new EventHandler(this.rb_CheckedChanged);
            this.panelTop.Controls.Add(this.gbAuthentication);
            this.panelTop.Controls.Add(this.panelSpace);
            this.panelTop.Controls.Add(this.gbCapabilitiesExchange);
            this.panelTop.Dock = DockStyle.Top;
            this.panelTop.Location = new Point(0, 0);
            this.panelTop.Name = "panelTop";
            this.panelTop.Padding = new Padding(0, 0, 0, 5);
            this.panelTop.Size = new Size(0x2e2, 0x33);
            this.panelTop.TabIndex = 3;
            this.panelSpace.Location = new Point(0x84, 0x13);
            this.panelSpace.Name = "panelSpace";
            this.panelSpace.Dock = DockStyle.Left;
            this.panelSpace.Size = new Size(7, 40);
            this.panelSpace.TabIndex = 2;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(this.tcDevice);
            base.Controls.Add(this.panelTop);
            base.Name = "DevicePage";
            base.Size = new Size(0x2e2, 0x1f3);
            this.tcDevice.ResumeLayout(false);
            this.tpDeviceManagement.ResumeLayout(false);
            this.tpMedia.ResumeLayout(false);
            this.tpPtz.ResumeLayout(false);
            this.tpRequests.ResumeLayout(false);
            this.gbCapabilitiesExchange.ResumeLayout(false);
            this.gbCapabilitiesExchange.PerformLayout();
            this.gbAuthentication.ResumeLayout(false);
            this.gbAuthentication.PerformLayout();
            this.panelTop.ResumeLayout(false);
            base.ResumeLayout(false);
        }

        private void rb_CheckedChanged(object sender, EventArgs e)
        {
            if (!this._notUserAction)
            {
                this._controller.UpdateContext();
                this.requestsPage.Controller.UpdateRequestSecurity();
            }
        }

        public override void SwitchToState(TestTool.GUI.Enums.ApplicationState state)
        {
            this.deviceManagementPage.SwitchToState(state);
            this.mediaPage.SwitchToState(state);
            this.ptzPage.SwitchToState(state);
        }

        public CapabilitiesExchangeStyle CapabilitiesExchange
        {
            get
            {
                if (!this.rbGetCapabilities.Checked)
                {
                    return CapabilitiesExchangeStyle.GetServices;
                }
                return CapabilitiesExchangeStyle.GetCapabilities;
            }
            set
            {
                this._notUserAction = true;
                this.rbGetCapabilities.Checked = value == CapabilitiesExchangeStyle.GetCapabilities;
                this._notUserAction = false;
            }
        }

        internal DeviceController Controller
        {
            get
            {
                return this._controller;
            }
        }

        public TestTool.HttpTransport.Interfaces.Security Security
        {
            get
            {
                if (this.rbNone.Checked)
                {
                    return TestTool.HttpTransport.Interfaces.Security.None;
                }
                if (this.rbDigest.Checked)
                {
                    return TestTool.HttpTransport.Interfaces.Security.Digest;
                }
                return TestTool.HttpTransport.Interfaces.Security.WS;
            }
            set
            {
                this._notUserAction = true;
                switch (value)
                {
                    case TestTool.HttpTransport.Interfaces.Security.None:
                        this.rbNone.Checked = true;
                        break;

                    case TestTool.HttpTransport.Interfaces.Security.Digest:
                        this.rbDigest.Checked = true;
                        break;

                    case TestTool.HttpTransport.Interfaces.Security.WS:
                        this.rbWsUsername.Checked = true;
                        break;
                }
                this._notUserAction = false;
            }
        }
    }
}

